from flask import Flask, render_template, request

from dataset import tokenizer
from model import model
import settings
import utils


app = Flask(__name__)
# 加载训练好的模型
model.load_weights(settings.BEST_MODEL_PATH)


@app.route('/')
def index():
    return render_template("index.html")


# TODO Generate a poem at random 随机生成一首诗
@app.route('/generate_a_poem_at_random')
def generate_a_poem_at_random():
    poetry = utils.generate_random_poetry(tokenizer, model)
    print(poetry)
    return poetry


# TODO With partial information given 给出部分信息的情况下，随机生成剩余部分
@app.route('/with_partial_information_given')
def with_partial_information_given():
    info = request.values.get("info")
    print(info)
    # 随机生成一首诗
    poetry = utils.generate_random_poetry(tokenizer, model, s=info)
    print(poetry)
    return poetry


# TODO Generate Tibetan Poems 生成藏头诗
@app.route('/generate_tibetan_poems')
def generate_tibetan_poems():
    info = request.values.get("info")
    print(info)
    # 随机生成一首诗
    poetry = utils.generate_acrostic(tokenizer, model, head=info)
    print(poetry)
    return poetry


if __name__ == '__main__':
    app.run(
        threaded=False,
        host='0.0.0.0',
        port=8082
    )
